from django import forms
from web import models
from web.forms.bootstrap import BootStrapForm


class WikiModelForm(BootStrapForm, forms.ModelForm):
    class Meta:
        model = models.Wiki
        fields = ['title', 'content', 'parent']

    # 重写init方法让Modelform在网页展示我们想要的数据
    def __init__(self, request, *args, **kwargs):
        super().__init__(*args, **kwargs)
        total_data_list = [("", "请选择")]
        # 问题:在选择父文章时其他项目的也一起展示出来
        # 解决:对文章进行查找，让他只显示当前项目的文章
        data_list = models.Wiki.objects.filter(project=request.tracer.project).values_list('id', 'title')
        total_data_list.extend(data_list)
        self.fields['parent'].choices = total_data_list
